﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra.Double;

namespace MTEA.LIB.Analysis
{
    public class BaseAnalysis
    {
        public static Tuple<double, double> CrossPoint(double x11, double y11, double x21, double y21, double x12, double y12, double x22, double y22)
        {
            var result = Solve(x11, y11, x21, y21);
            double a1 = result.Item1;
            double b1 = result.Item2;

            //Console.WriteLine("a1: {0} b1: {1}", a1, b1);

            var result1 = Solve(x12, y12, x22, y22);
            double a2 = result1.Item1;
            double b2 = result1.Item2;

            //Console.WriteLine("a2: {0} b2: {1}", a2, b2);

            var matrixA = DenseMatrix.OfArray(new[,] { { a1, -1.00 }, { a2, -1.00 } });
            var vectorB = new DenseVector(new[] { -b1, -b2 });
            var resultX = matrixA.LU().Solve(vectorB);
            return new Tuple<double, double>(resultX[0], resultX[1]);
        }
        static Tuple<double, double> Solve(double x1, double y1, double x2, double y2)
        {
            var matrixA = DenseMatrix.OfArray(new[,] { { x1, 1.00, }, { x2, 1.00 } });
            var vectorB = new DenseVector(new[] { y1, y2 });
            var resultX = matrixA.LU().Solve(vectorB);
            // Console.WriteLine(resultX);
            return new Tuple<double, double>(resultX[0], resultX[1]);
        }

    }
}
